package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.StudentsInformationDao;
import Entity.Student;

/**
 * Servlet implementation class PageServlet
 */
//@WebServlet("/PageServlet")
public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currPage=1;   //当前页码
		if(request.getParameter("page")!=null) {   //判断传递页面是否有效
			currPage=Integer.parseInt(request.getParameter("page"));   //对当前页码赋值
		}
		StudentsInformationDao dao=new StudentsInformationDao();
		List<Student> slist=dao.showAllStudentsByPage(currPage);
		request.setAttribute("slist", slist);
		int pages;
		int count=dao.queryRecordCount();
		if(count%Student.PAGE_SIZE==0) {
			pages=count/Student.PAGE_SIZE;
		}else {
			pages=count/Student.PAGE_SIZE+1;
		}
		StringBuffer sb=new StringBuffer();
		for(int i=1;i<=pages;i++) {
			if(i==currPage) {
				sb.append("["+i+"]");
			}else {
				sb.append("<a href='PageServlet?page="+i+"'>"+i+"</a>");   //构建分页条
			}
			sb.append(" ");
		}
		request.setAttribute("bar", sb.toString());
		request.getRequestDispatcher("administratorPage.jsp").forward(request, response);//转发到administratorPage.jsp页面
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
